<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/css/layui.css">
    <link rel="stylesheet" href="/css/admin/index.css">
</head>
<body>
<div class="main">
{{include './header.html'}}
    <div class="content-box">
        <div class="listwrap">
            <ul>
                {{each results}}
                <li>
                    <div class="list-id">
                        {{$value.id}}
                    </div>
                    <div class="list-img">
                        <img src="{{$value.thumb}}" alt="" width="240">
                    </div>
                    <div class="list-title">
                        <a href="/admin/news?id={{$value.id}}">{{$value.title}}</a>
                    </div>
                    <div class="list-type">
                        {{$value.type}}
                    </div>
                    <div class="list-origin">
                        {{$value.origin}}
                    </div>
                    <div class="list-fn-box">
                        <a href="javascript:;" data-id="{{$value.id}}" class="deletenews">删除</a>
                    </div>
                </li>
                {{/each}}
            </ul>
        </div>
        <div class="page-box" id="pagebox" data-page="{{ count.total }}">

        </div>
    </div>
</div>
{{include './footer.html'}}
<script>
$(function(){
    var typeJson = {
        1 : '白',
        2 : '黑',
        3 : 'JK',
        4 : '幽默段子'
    }
    var deleteSwitch = true;
    $('.deletenews').click(function(){
        if(deleteSwitch){
            deleteSwitch = false;
            var id = $(this).attr('data-id');
            var r = window.confirm('确定要删除本条新闻吗？');
            var _this = $(this);
            if(r){
                $.ajax({
                    url:'/deleteNews',
                    type:'post',
                    data:JSON.stringify({
                        id:id
                    }),
                    contentType: 'application/json',
                    success:function(d){
                        _this.parents('li').remove();
                        deleteSwitch = true;
                    }
                })
            }
        }
    });
    $('.list-type').each(function(){
        var str = typeJson[$.trim($(this).text())];
        $(this).text(str)
    })
    layui.use('laypage', function(){
        var laypage = layui.laypage;
        var currPage = window.location.search.split('=');
        //执行一个laypage实例
        laypage.render({
            elem: 'pagebox', //注意，这里的 test1 是 ID，不用加 # 号
            count: $('#pagebox').attr('data-page'), //数据总数，从服务端得到
            limit : 20,
            groups:5,
            curr: currPage[1],
            jump : function(obj, first){
                console.log(first)
                if(!first){
                    window.location.href = '/admin/newsList?page=' + obj.curr;
                }
            }
        });
    });
})
</script>
</body>
</html>